from time import sleep

def addUser(wd, users, data):
    print("添加乘车人")
    print("总计添加", len(users), "人")
    count = 1
    for user in users:
        print("正在添加第", count, "个人： ", user)
        # 点击乘车人
        select(wd, data[8]).click()

        # 点击添加
        select(wd, data[9]).click()

        # 添加数据
        select(wd, data[10]).send_keys(user[0])
        select(wd, data[11]).send_keys(user[1])
        select(wd, data[12]).send_keys(user[2])

        # 点击保存按钮
        select(wd, data[13]).click()
        sleep(2)
        # 点击确定按钮
        select(wd, data[14]).click()
        count = count + 1


# 以下为模块测试代码
if __name__ == '__main__':
    print('test_addUser')
    from selenium import webdriver
    import readcsv as Csv
    import readjson as Json
    from selector import select
    from login import login, verificationCode

    wait_time = 1

    # 读取csv文件
    user = Json.read("../config/user.json")
    add_user = Csv.read("../config/adduser.csv")
    data = Csv.read("../config/data.csv")
    # 避免webdriver检测
    options = webdriver.ChromeOptions()
    options.add_argument('--disable-blink-features=AutomationControlled')
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    driver = webdriver.Chrome(options=options)
    # 设置隐式等待为10秒
    driver.implicitly_wait(5)
    # 打开12306网站
    driver.get("https://www.12306.cn/")
    # 设置浏览器全屏
    driver.maximize_window()
    # 点击登录
    select(driver, data[0]).click()
    # 登录模块
    login(driver, user, data)
    sleep(wait_time)
    # 验证码
    verificationCode(driver, data)
    sleep(wait_time)
    # 点击疫情确认按钮
    select(driver, data[7]).click()
    # 添加联系人模块
    addUser(driver, add_user, data)
    sleep(wait_time)
    # 退出浏览器
    sleep(wait_time)
    driver.quit()
    print("关闭浏览器")
else:
    from tools.selector import select
